"use strict";
cc._RF.push(module, '092af/2BZlPjY+nAg7SFRVb', 'Counter');
// eazax-ccc/components/Counter.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property, requireComponent = _a.requireComponent;
var Counter = /** @class */ (function (_super) {
    __extends(Counter, _super);
    function Counter() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.time = 1;
        _this.keepInteger = true;
        _this.label = null;
        _this._value = 0;
        _this.tween = null;
        _this.lastTarget = 0;
        return _this;
    }
    Object.defineProperty(Counter.prototype, "value", {
        /**
         * 数值
         */
        get: function () { return this._value; },
        set: function (value) {
            if (this.keepInteger)
                value = Math.floor(value);
            this._value = value;
            this.label.string = value.toString();
        },
        enumerable: false,
        configurable: true
    });
    Counter.prototype.onLoad = function () {
        this.init();
    };
    /**
     * 初始化组件
     */
    Counter.prototype.init = function () {
        this.label = this.getComponent(cc.Label);
        this.value = 0;
    };
    /**
     * 设置数值
     * @param value 数值
     */
    Counter.prototype.setValue = function (value) {
        this.value = value;
    };
    /**
     * 设置时间
     * @param time 时间
     */
    Counter.prototype.setTime = function (time) {
        this.time = time;
    };
    /**
     * 滚动数值
     * @param target 目标值
     * @param time 时间
     * @param callback 完成回调
     */
    Counter.prototype.to = function (target, time, callback) {
        var _this = this;
        if (time === void 0) { time = null; }
        return new Promise(function (res) {
            if (_this.tween) {
                _this.tween.stop();
                _this.tween = null;
            }
            if (time !== null) {
                _this.time = time;
            }
            _this.lastTarget = target;
            _this.tween = cc.tween(_this)
                .to(_this.time, { value: target })
                .call(function () {
                callback && callback();
                _this.tween = null;
                res();
            })
                .start();
        });
    };
    /**
     * 相对滚动数值
     * @param diff 差值
     * @param time 时间
     * @param callback 完成回调
     */
    Counter.prototype.by = function (diff, time, callback) {
        var _this = this;
        if (time === void 0) { time = null; }
        return new Promise(function (res) {
            if (_this.tween) {
                _this.tween.stop();
                _this.tween = null;
                _this.value = _this.lastTarget;
            }
            if (time !== null) {
                _this.time = time;
            }
            _this.lastTarget = _this.value + diff;
            _this.tween = cc.tween(_this)
                .to(_this.time, { value: _this.lastTarget })
                .call(function () {
                callback && callback();
                _this.tween = null;
                res();
            })
                .start();
        });
    };
    __decorate([
        property({ tooltip: CC_DEV && '动作时间' })
    ], Counter.prototype, "time", void 0);
    __decorate([
        property({ tooltip: CC_DEV && '保持数值为整数' })
    ], Counter.prototype, "keepInteger", void 0);
    Counter = __decorate([
        ccclass,
        requireComponent(cc.Label)
    ], Counter);
    return Counter;
}(cc.Component));
exports.default = Counter;

cc._RF.pop();